import time

from selenium import webdriver
import pytest,allure
from selenium.webdriver.common.by import By
import uuid



@allure.feature("CRMEB测试")
class Test_Crmeb():
    uuid = str(uuid.uuid4()).split('-')[0]

    @pytest.fixture(scope='function',autouse=True)
    def setup_teardown(self):
        option = webdriver.ChromeOptions()  # todo：chrome浏览器配置项
        option.add_experimental_option("detach", True)  # todo：浏览器不会自动关闭
        self.driver = webdriver.Chrome(executable_path=r"chromedriver.exe", options=option)
        self.driver.get("http://123.56.16.34:8899/admin/login")
        self.driver.implicitly_wait(15)  # 设置隐式等待15s
        self.driver.maximize_window()
        # 输入账号，密码
        inputs = self.driver.find_elements(By.TAG_NAME,"input")
        for i in inputs:
            if i.get_attribute("placeholder") =="请输入用户名":
                i.clear()
                i.send_keys("admin")
            elif i.get_attribute("placeholder") == "请输入密码":
                i.clear()
                i.send_keys("123456")
            elif i.get_attribute("placeholder") =="请输入验证码":
                i.clear()
                i.send_keys("a")
        #点击登录按钮
        self.driver.find_elements(By.CLASS_NAME,"ivu-form-item-content")[3].click()

    @allure.story("用户分组测试")
    def test_user_group(self):
        try:
            #点击左侧导航栏—用户
            self.driver.find_elements(By.CLASS_NAME,"ivu-menu-submenu-title")[2].click()
            # 点击用户分组
            self.driver.find_elements(By.CLASS_NAME,"ivu-menu-item")[9].click()
            # 点击添加分组
            self.driver.find_element(By.XPATH,"//*[@id='app']/div/div[2]/div[2]/div/div[2]/div/div[2]/div/div[1]/div/button/span").click()
            # 等待弹窗出现
            time.sleep(2)
            # 输入分组名称
            self.driver.find_elements(By.CLASS_NAME,"ivu-input")[0].send_keys("刘闯sb | " + str(self.uuid))
            # 点击提交
            self.driver.find_elements(By.CLASS_NAME,"ivu-btn")[4].click()
            time.sleep(3)
            # 定位到删除的父级元素,tr
            ivu_table = self.driver.find_elements(By.CLASS_NAME,"ivu-table-cell-slot")[-1]
            # 收集所有的a标签
            a = ivu_table.find_elements(By.TAG_NAME,'a')
            for i in a:
                if i.text == "删除1":
                    i.click()
            # 等待弹窗出现
            time.sleep(1)
            # 点击删除按钮
            self.driver.find_elements(By.CLASS_NAME,"ivu-btn")[5].click()
        except:
            self.driver.get_screenshot_as_file("./error.png")
            raise "出现错误异常"



